package com.funky.service.impl;



import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.funky.pojo.User;
import com.funky.dao.UserMapper;
import com.funky.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaojin
 * @since 2021-01-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

  @Autowired
  private UserMapper usersMapper;

  @Override
  public List<User> findAllUser() {
    return usersMapper.selectList(null);
  }

  @Override
  public Integer register(User user) {
    String md5 = SecureUtil.md5(user.getPassword());
    user.setPassword(md5);
    return usersMapper.insert(user);
  }

  @Override
  public User login(User user) {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    String md5 = SecureUtil.md5(user.getPassword());
    queryWrapper.eq("email",user.getEmail())
            .eq("password",md5);
    return usersMapper.selectOne(queryWrapper);
  }

  @Override
  public User findUserById(String id) {
    return usersMapper.selectById(id);
  }

  @Override
  public boolean findByEmail(String email) {
    HashMap<String, Object> map = new HashMap<>();
    map.put("email",email);
    List<User> list = usersMapper.selectByMap(map);
    if(list.size()>0){
      return false;
    }
    return true;
  }
}
